package com.yunhe.common.model;

import com.yunhe.common.model.mybatis.PageResult;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * JSON结果返回对象
 *
 * @author liuronglei
 * @desc JSON结果返回对象定义
 */
//@JsonInclude(JsonInclude.Include.NON_NULL)
@ResponseBody
public class ResultObject<T> {
    //操作结果对象
    //如果只有一个值，如通过id获取对象，则为对象Object，如果可能有多个值，如查询，结果是个Iterable，如果带分页，则为Page
    private T results;
    //当前页，从1开始
    private Integer page;
    //每页数量，默认数量在config/systemoption.properties中配置
    private Integer size;
    //结果总数量
    private Long totalCount;
    //结果总页数
    private Integer totalPages;
    /*
    //上一页链接，暂未实现
    private String prevPageLink;
    //下一页链接，暂未实现
    private String nextPageLink;
    */
    //错误代码，没有错误的话，为0
    private int errorCode;
    //错误消息，没有错误的话，为空
    private String errorMsg;

    public ResultObject() {
    }

    public ResultObject(T results) {
        if (results instanceof Page) {
            Page pageObj = (Page) results;
            this.results = (T) pageObj.getContent();
            this.page = pageObj.getNumber()+1;
            this.size = pageObj.getSize();
            this.totalCount = pageObj.getTotalElements();
            this.totalPages = pageObj.getTotalPages();
        } else if (results instanceof Iterable) {
            this.results = results;
            this.page = 0;
            this.totalCount = (long) ((List) results).size();
            this.totalPages = 1;
            this.size = this.totalCount.intValue();
        } else if (results instanceof PageResult) {
            PageResult pageResult = (PageResult) results;
            this.results = (T)pageResult.getRecords();
            this.page = pageResult.getPageIndex();
            this.totalCount = (long) pageResult.getTotal();
            this.totalPages = pageResult.getTotalPage();
            this.size = pageResult.getPageSize();
        } else {
            this.results = results;
            this.page = 0;
            this.totalCount = 0L;
            this.totalPages = 0;
            this.size = 0;
        }
    }

    public ResultObject(int errorCode, String errorMsg) {
        this.errorCode = errorCode;
        this.errorMsg = errorMsg;
    }

    public ResultObject(T results, int page, int size, Long totalCount, int totalPages) {
        this.results = results;
        this.page = page;
        this.size = size;
        this.totalCount = totalCount;
        this.totalPages = totalPages;
    }

    public T getResults() {
        return results;
    }

    public void setResults(T results) {
        this.results = results;
    }

    public int getErrorCode() {
        return errorCode;
    }

    public void setErrorCode(int errorCode) {
        this.errorCode = errorCode;
    }

    public String getErrorMsg() {
        return errorMsg;
    }

    public void setErrorMsg(String errorMsg) {
        this.errorMsg = errorMsg;
    }

    public Integer getPage() {
        return page;
    }

    public void setPage(Integer page) {
        this.page = page;
    }

    public Integer getSize() {
        return size;
    }

    public void setSize(Integer size) {
        this.size = size;
    }

    public Long getTotalCount() {
        return totalCount;
    }

    public void setTotalCount(Long totalCount) {
        this.totalCount = totalCount;
    }

    public Integer getTotalPages() {
        return totalPages;
    }

    public void setTotalPages(Integer totalPages) {
        this.totalPages = totalPages;
    }

    /*
    public String getPrevPageLink() {
        return prevPageLink;
    }

    public void setPrevPageLink(String prevPageLink) {
        this.prevPageLink = prevPageLink;
    }

    public String getNextPageLink() {
        return nextPageLink;
    }

    public void setNextPageLink(String nextPageLink) {
        this.nextPageLink = nextPageLink;
    }
    */
}
